package com.example.controller.admin;

import com.example.dto.ProductPageQueryDTO;
import com.example.dto.ProductsDTO;
import com.example.entity.Products;
import com.example.entity.Store;
import com.example.result.PageResult;
import com.example.result.Result;
import com.example.service.ProductsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * (Products)表控制层
 *
 * @author makejava
 * @since 2024-03-21 20:13:56
 */
@RestController
@RequestMapping("/admin/product")
@Slf4j
@Api(tags = "管理页面商品相关接口")
public class ProductsController {
    /**
     * 服务对象
     */
    @Resource
    private ProductsService productsService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 分页查询
     *
     * @param products 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<Products>> queryByPage(Products products, PageRequest pageRequest) {
        return ResponseEntity.ok(this.productsService.queryByPage(products, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询商品")
    public Result<Products> queryById(@PathVariable("id") Long id) {
        return Result.success(this.productsService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param productsDTO 实体
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation("添加商品")
    public Result<Products> add(@RequestBody ProductsDTO productsDTO) {
        log.info("新增商品:{}",productsDTO);
        return Result.success(productsService.insert(productsDTO));
    }

    @GetMapping("/page")
    @ApiOperation("商品分类查询")
    public Result<PageResult> page(ProductPageQueryDTO productPageQueryDTO){
        log.info("商品分类查询，{}",productPageQueryDTO);
        PageResult pageResult = productsService.pageQuery(productPageQueryDTO);
        return Result.success(pageResult);
    }

    @DeleteMapping
    @ApiOperation("商品批量删除")
    public Result delete(@RequestParam List<Long> ids){
        log.info("商品批量删除：{}",ids);
        Result result = productsService.delete(ids);
        cleanCache("product_*"); //可能会有bug
        return result;
    }

    @PostMapping("/status/{status}")
    @ApiOperation("商品的起售和停售")
    public Result<String> startOrStop(@PathVariable Integer status,Long id){
        productsService.startOrStop(status,id);
        cleanCache("product_*");
        return Result.success();
    }


    @PutMapping
    @ApiOperation("修改商品")
    public Result<Products> update(@RequestBody ProductsDTO productsDTO){
        log.info("修改商品：{}",productsDTO);
        Products products = productsService.update(productsDTO);
        cleanCache("product_*");
        return Result.success(products);
    }



    @GetMapping("/list")
    @ApiOperation("根据目录id查询菜品列表")
    public Result<List<Products>> list(Integer categoryId){
        log.info("根据目录id查询菜品列表：{}",categoryId);
        List<Products> products =  productsService.list(categoryId);
        return Result.success(products);
    }


    //清除redis缓存
    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }


}

